package jpsearchcore;

import java.util.ArrayList;
import java.util.List;

public class ContentDesciption {

	private List<CDPerson> person = new ArrayList<CDPerson>();
	private List<CDObject> object = new ArrayList<CDObject>();
	private List<Place> place = new ArrayList<Place>();
	private List<CDEvent> event = new ArrayList<CDEvent>();

	public ContentDesciption(){}

	public ContentDesciption(jpsearchcore.RegionOfInterestType.ContentDescription cd){
		//Person
		List<PersonType> pt = cd.getPerson();
		for(PersonType details : pt){
			person.add(new CDPerson(details));
		}

		//Object
		List<ObjectType> objT = cd.getObject();
		for(ObjectType ot : objT){
			object.add(new CDObject(ot));
		}

		//Place
		List<PlaceType> placeT = cd.getPlace();		
		for(PlaceType pType : placeT){						
			place.add(new Place("ContentDescription Place", pType));
		}

		//Event
		List<EventType> evenT = cd.getEvent();
		for(EventType e : evenT){			
			event.add(new CDEvent(e));
		}	
	}

	public ContentDesciption(List<CDPerson> person, List<CDObject> object,
			List<Place> place, List<CDEvent> event) {
		this.person = person;
		this.object = object;
		this.place = place;
		this.event = event;
	}

	public List<CDPerson> getPerson() {
		return person;
	}

	public List<CDObject> getObject() {
		return object;
	}

	public List<Place> getPlace() {
		return place;
	}

	public List<CDEvent> getEvent() {
		return event;
	}

	@Override
	public String toString() {
		return "ContentDesciption [person=" + person + ", object=" + object
				+ ", place=" + place + ", event=" + event + "]";
	}

	//Content Description Person class
	public static class CDPerson{

		private Person Names;
		private List<CDOrganizationType> Affiliation = new ArrayList<CDOrganizationType>();
		private Place Address;
		private String Description = "";
		private String Nationality = "";

		public CDPerson(){}

		public CDPerson(PersonType details){
			try{
				Names = new Person(details.getName());
			}catch(NullPointerException e){}			

			//Person/Affiliations
			try{
				for(OrganizationType ot : details.getAffiliation()){				
					Affiliation.add(new CDOrganizationType(ot));
				}
			}catch(NullPointerException e){}			

			//Person/Address
			try{
				Address = new Place("Person/Address", details.getAddress());
			}catch(NullPointerException e){}			

			//Person/Description
			try{
				if(details.getDescription() != null)
					Description = details.getDescription();
			}catch(NullPointerException e){}

			//Person/Nationality
			try{
				if(details.getNationality() != null)
					Nationality = details.getNationality();
			}catch(NullPointerException e){}

		}

		public CDPerson(Person names, List<CDOrganizationType> affiliation,
				Place address, String description, String nationality) {
			Names = names;
			Affiliation = affiliation;
			Address = address;
			Description = description;
			Nationality = nationality;
		}
		public Person getNames() {
			return Names;
		}
		public void setNames(Person names) {
			Names = names;
		}
		public void setGivenName(String gn){
			if(Names == null)
				Names = new Person();
			Names.setGivenName(gn);
		}
		public void setFamilyName(String fn){
			if(Names == null)
				Names = new Person();
			Names.setFamilyName(fn);
		}
		public List<CDOrganizationType> getAffiliation() {
			return Affiliation;
		}
		public void setAffiliation(List<CDOrganizationType> affiliation) {
			Affiliation = affiliation;
		}
		public Place getAddress() {
			return Address;
		}
		public void setAddress(Place address) {
			Address = address;
		}
		
		//****** Setting Address name and description ********
		//The address when read is obtained as a String containing
		//the whole address. When saved, it is handled as a list:
		//clearing the list and adding it as an item in list.
		public void setAddressName(String name){
			Address.getName().clear();
			Address.getName().add(name);			
		}
		public void setAddressDescription(String descr){
			Address.getDescription().clear();
			Address.getDescription().add(descr);			
		}
		public String getDescription() {
			return Description;
		}
		public void setDescription(String description) {
			Description = description;
		}
		public String getNationality() {
			return Nationality;
		}
		public void setNationality(String nationality) {
			Nationality = nationality;
		}
		@Override
		public String toString() {
			return "CDPerson [Names=" + Names + ", Affiliation=" + Affiliation
					+ ", Address=" + Address + ", Description=" + Description
					+ ", Nationality=" + Nationality + "]";
		}
	}

	//ContentDesciption Organization Type
	public static class CDOrganizationType{

		private List<String> Name = new ArrayList<String>();
		private Place Address;

		public CDOrganizationType(){}

		public CDOrganizationType(OrganizationType ot){
			//Person/Affiliations/Name
			Name.addAll(ot.getName()); //Name [0..inf]

			//Person/Affiliations/Address
			Address = new Place("Person/Affiliations/Address", ot.getAddress());
		}

		public CDOrganizationType(List<String> name, Place address) {
			Name = name;
			Address = address;
		}
		
		public CDOrganizationType(String name, Place address) {
			Name.add(name);
			Address = address;
		}
		
		public List<String> getNames() {
			return Name;
		}

		public String getName() {
			if(Name.isEmpty())
				return "";
			String res = "";
			for(String s : Name){
				res += s + " ";
			}

			return res.substring(0, res.length()-1);
		}
		public void setName(List<String> name) {
			Name = name;
		}
		public Place getAddress() {
			return Address;
		}
		public String getAddressName(){
			if(Address.getName().isEmpty())
				return "";
			String res = "";
			for(String s : Address.getName()){
				res += s + " ";
			}

			return res.substring(0, res.length()-1);
		}
		public String getAddressDescription(){
			if(Address.getDescription().isEmpty())
				return "";
			
			String res = "";
			for(String s : Address.getDescription()){
				res += s + " ";
			}

			return res.substring(0, res.length()-1);
		}
		public void setAddress(Place address) {
			Address = address;
		}

		@Override
		public String toString() {
			return "CDOrganizationType [Name=" + Name + ", Address=" + Address + "]";
		}
	}

	//Content Description Object class
	public static class CDObject{

		@Override
		public String toString() {
			return "CDObject [Name=" + Name + ", Description=" + Description
					+ ", Label=" + Label + "]";
		}

		private List<String> Name = new ArrayList<String>();
		private String Description = "";
		private String Label = "";

		public CDObject(){}

		public CDObject(ObjectType ot){
			try{
				Name.addAll(ot.getName());
			}catch(NullPointerException e){};	
			////
			try{
				Description = ot.getDescription().trim();
			}catch(NullPointerException e){};
			////
			try{
				Label = ot.getLabel().trim();				
			}catch(NullPointerException e){};
		}

		public CDObject(List<String> name, String description, String label) {
			Name = name;
			Description = description;
			Label = label;
		}

		public CDObject(String description, String label) {
			Description = description;
			Label = label;
		}

		public List<String> getName() {
			return Name;
		}

		public void setName(List<String> name) {
			Name = name;
		}

		public String getDescription() {
			return Description;
		}

		public void setDescription(String description) {
			Description = description;
		}

		public String getLabel() {
			return Label;
		}

		public void setLabel(String label) {
			Label = label;
		}
	}

	//Content Description Event class
	public static class CDEvent{
		List<String> Label = new ArrayList<String>();
		List<String> Description = new ArrayList<String>();

		public CDEvent(){}

		public CDEvent(EventType e){
			Label.addAll(e.getLabel());
			for(String l : Label){
				l.trim();
			}

			Description.addAll(e.getDescription());
			for(String d : Description){
				d.trim();
			}
		}

		public CDEvent(String label, String description){
			if(label!=null)
				Label.add(label);
			if(description!=null){
				Description.add(description);
			}
		}

		public CDEvent(List<String> label, List<String> description) {
			Label = label;
			Description = description;
		}

		@Override
		public String toString() {
			return "CDEvent [Label=" + Label + ", Description=" + Description
					+ "]";
		}

		public List<String> getLabel() {
			return Label;
		}

		public void setLabel(List<String> label) {
			Label = label;
		}

		public List<String> getDescription() {
			return Description;
		}

		public void setDescription(List<String> description) {
			Description = description;
		}
	}
}
